//
//  Service+SPOrder.m
//  BaseProject
//
//  Created by Sailer Guo on 2019/12/17.
//  Copyright © 2019 Beijing Shopin Retail Development Co., Ltd. All rights reserved.
//

#import "HTTPService+SPOrder.h"
#import "EnvironmentConstants.h"

@implementation HTTPService (SPOrder)

- (RACSignal *)selectOrderListByParamToWeChatType:(NSString *)type pageNum:(NSInteger)pageNum {
    NSDictionary *parameters = @{
        @"memberSid":[self memberSid],
        @"fetchSize":@(10),
        @"pageNumber":@(pageNum),
        @"type":type
    };
    
    NSArray *necessaryKeys = @[@"memberSid",@"type"]; // 必须的参数
    
    // 校验是否缺少必须参数
    NSAssert([self didParameters:parameters containNecessaryKeys:necessaryKeys], @"缺少必要参数");
    
    NSString *url = @"order/selectOrderListByParamToWeChat";
    
    RACTuple *tuple = [RACTuple tupleWithObjects:@(POST),url,parameters,@(HTTPServiceTypeCenter), nil];
    
    return [self signalProduce:tuple withDecodeClass:nil isList:YES];
}

- (RACSignal *)selectRefundApplyByParamToWeChat:(NSInteger)pageNum {
    
    NSDictionary *parameters = @{
        @"memberSid":[self memberSid],
        @"fetchSize":@(10),
        @"pageNumber":@(pageNum)
    };
    NSArray *necessaryKeys = @[@"memberSid"]; // 必须的参数
    // 校验是否缺少必须参数
    NSAssert([self didParameters:parameters containNecessaryKeys:necessaryKeys], @"缺少必要参数");
    NSString *url = @"refund/selectRefundApplyByParamToWeChat";
    
    
    RACTuple *tuple = [RACTuple tupleWithObjects:@(POST),url,parameters,@(HTTPServiceTypeCenter), nil];
    
    return [self signalProduce:tuple withDecodeClass:nil isList:YES];
}


- (RACSignal *)getSignData:(NSString *)orderNo payType:(SPPayType)payType {
    NSDictionary *parameters = @{
        @"orderNo":orderNo,
        @"type":@(payType)
    };
    
    NSArray *necessaryKeys = @[@"orderNo",@"type"]; // 必须的参数
    // 校验是否缺少必须参数
    NSAssert([self didParameters:parameters containNecessaryKeys:necessaryKeys], @"缺少必要参数");
    NSString *url = @"pay/getSign";
    url = [url stringByAppendingFormat:@"/%@/%@",orderNo,@(payType)];
    RACTuple *tuple = [RACTuple tupleWithObjects:@(POST),url,@{},@(HTTPServiceTypeCenter), nil];
    return [self signalProduce:tuple withDecodeClass:nil isList:NO];
}

- (RACSignal *)checkOrder:(NSString *)orderNo payType:(SPPayType)payType {
    NSDictionary *parameters = @{
        @"orderNo":orderNo,
        @"type":@(payType)
    };
    
    NSArray *necessaryKeys = @[@"orderNo",@"type"]; // 必须的参数
    // 校验是否缺少必须参数
    NSAssert([self didParameters:parameters containNecessaryKeys:necessaryKeys], @"缺少必要参数");
    NSString *url = @"pay/check";
    url = [url stringByAppendingFormat:@"/%@/%@",orderNo,@(payType)];
    RACTuple *tuple = [RACTuple tupleWithObjects:@(POST),url,@{},@(HTTPServiceTypeCenter), nil];
    return [self signalProduce:tuple withDecodeClass:nil isList:NO];
}

- (RACSignal *)modifyOrder:(NSString *)orderNo flag:(NSNumber *)flag {
    
    NSDictionary *parameters = @{
        @"orderNo":orderNo,
        @"recoveryFlag":flag,
        @"memberSid":[self memberSid]
    };
    
    NSArray *necessaryKeys = @[@"orderNo",@"recoveryFlag",@"memberSid"]; // 必须的参数
    // 校验是否缺少必须参数
    NSAssert([self didParameters:parameters containNecessaryKeys:necessaryKeys], @"缺少必要参数");
    NSString *url = @"order/modifyRecoveryFlag";
    RACTuple *tuple = [RACTuple tupleWithObjects:@(POST),url,parameters,@(HTTPServiceTypeCenter), nil];
    
    return [self signalProduce:tuple withDecodeClass:nil isList:NO];
}


- (NSString *)memberSid {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *memberSid = [userDefaults objectForKey:kMemberSid];
    if (memberSid) {
        return memberSid;
    }
    // 这里要怎么处理就看后台设计了
    return @"";
}

@end
